package com.bo.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.bo.entity.Comment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class OnlineUtil {
	
	private static final String BASE_URL="http://10.0.2.2:8080/MyProject/";
	
	private static final String BASE_IMAGE_URL=BASE_URL+"image/";
	
	private static final String BASE_XML_URL=BASE_URL+"xml/";
	
	private static final String BASE_COMMENT_URL=BASE_URL;
	
	
	public  static String getXMLString(String dir,String id,int page,int offset){
			
		String urlStr=BASE_XML_URL+dir+"/xml.do";
		
		
		try{
		
		  if(dir.equalsIgnoreCase(LoadResourceProxy.COMPANY_LIST)){
			
			   urlStr+="?page="+page+"&offset="+offset+"&search="+new String(id.getBytes("gbk"),"iso-8859-1");
			
		  }else if(dir.equalsIgnoreCase(LoadResourceProxy.PRODUCT)){
			
			   urlStr+="?productId="+id;
		
		  }else if(dir.equalsIgnoreCase(LoadResourceProxy.PRODUCT_LIST)){
		  
			    urlStr+="?companyId="+id+"&page="+page+"&offset="+offset;
		  
		  }else if(dir.equalsIgnoreCase(LoadResourceProxy.PRODUCT_COMMENT)){
			  
			  urlStr+="?productId="+id;
		  
		  }else{
			  
		       urlStr+="?companyId="+id;
		  }
			
		    URL url = new URL(urlStr);
		        
		    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			
		    if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
					
					InputStream is=conn.getInputStream();
					
					byte[] buffer = new byte[is.available()];
					
					is.read(buffer);
					
					String msg = new String(buffer);
					
					return msg;
			}
		             
		}catch(Exception e){
					
			Log.i("error",e.getMessage());
		}

		return null;
	}
	  
	  
	  
	  
	public static Bitmap getImage(String dir,String id,int index){
			
		String urlStr=BASE_IMAGE_URL+dir+"/Img.do";
		
		urlStr+="?id="+id+"&index="+index;
		
		if(dir.equalsIgnoreCase(LoadResourceProxy.PRODUCT_LIST)){
			
			urlStr+="&width="+120+"&height="+120;
		
		}else if(dir.equalsIgnoreCase(LoadResourceProxy.COMPANY_LIST)){
			
			urlStr+="&width="+100+"&height="+100;
		}
		

			
		try{
			   
			   URL url = new URL(urlStr);
		        
		       HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			
			   if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
					
				   InputStream is=conn.getInputStream();
			   
			       return BitmapFactory.decodeStream(is);
			  }
			
		   }catch(Exception e){
			   
			   Log.i("error",e.getMessage());
		   }
			
		   return null;
	}
	
	
	
	
	public static boolean submitComment(Comment comment){
		
		String urlStr=BASE_COMMENT_URL+"product_comment.do";
		
		HttpPost request=new HttpPost(urlStr);
		
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		
		params.add(new BasicNameValuePair("rate",""+comment.getRate()));
		
		params.add(new BasicNameValuePair("content",comment.getContent()));
	
		params.add(new BasicNameValuePair("productId",comment.getProductId()));
		
	    try{
	    	
	    	request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
	    	
	    	HttpResponse response = new DefaultHttpClient().execute(request);
	    
	        if(response.getStatusLine().getStatusCode()==200){
	        	
	        	
	        	
	        	return true;
	        }
	    
	    }catch(Exception e){
	    	
	    	Log.i("error",e.getStackTrace().toString());
	    }
	
	   return false;
	
	}
	

}
